<template>
  <div class="wrap">
    <!-- 初始选中没有做 -->
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      status-icon
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="用户登录：" prop="need_token">
        <el-radio-group v-model="ruleForm.need_token">
          <el-radio :value="0">无需登录</el-radio>
          <el-radio :value="1">需要登录</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          修改
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()
const { proxy } = getCurrentInstance()

let ruleFormRef = ref(null)
const app_id = route.query.app_id
const need_token = route.query.need_token

const ruleForm = reactive({
  need_token: 1,
})

console.log(ruleForm.need_token);

const rules = reactive({
  need_token: [{ required: false, message: "请选择用户登录", trigger: "blur" }]
})



const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/dev/app/update/needtoken', {
        app_id: app_id,
        need_token: ruleForm.need_token,
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            setTimeout(() => {
              router.push('/application')
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  ruleForm.need_token = need_token
 
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}

.btn-wrap {
  margin-top: 30px;
}
</style>
